﻿using DotNetCore.CAP;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using X23.Application.TestManagement;
using X23.Application.UserManagement;
using X23.Application.UserManagement.Dto;
using X23.ApplicationJob.Application;
using X23.ApplicationJob.Application.Dto;
using X23.Extensions.AttributeExtensions;
using X23.Model.Const;
using X23.Util.SerializationUtil;

namespace X23.Controllers
{
    /// <summary>
    /// 测试管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TestController : BaseApiController
    {
        private readonly ICapPublisher _capPublisher;
        private readonly IUserManagementService _userManagementService;
        private readonly ITestManagementService _testManagementService;
        private readonly IServiceProvider _serviceProvider;
        private readonly IJobService _jobService;

        public TestController(ICapPublisher capPublisher, IUserManagementService userManagementService, ITestManagementService testManagementService,IServiceProvider serviceProvider, IJobService jobService)
        {
            _capPublisher = capPublisher;
            _userManagementService = userManagementService;
            _testManagementService = testManagementService;
            _serviceProvider = serviceProvider;
            _jobService = jobService;
        }

        #region 测试定时作业
        /// <summary>
        /// GetAllJob
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public List<AllJobDto> GetAllJob()
        {
            return  _jobService.GetAllJob();
        }
        /// <summary>
        /// RunJob
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public bool RunJob(string key)
        {
            return  _jobService.RunJob(key);
        }
        /// <summary>
        /// RemoveJob
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public bool RemoveJob(string key)
        {
            return _jobService.RemoveJob(key);
        }
        /// <summary>
        /// TriggerJob
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public bool TriggerJob(string key)
        {
            return _jobService.TriggerJob(key);
        }
        #endregion

        /// <summary>
        /// 测试权限查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        [X23Authorize(nameof(TestController), ButtonConst.Query)]
        public async Task<bool> TestAuthByQueryData()
        {
            return await Task.FromResult(true);
        }
        [HttpGet]
        [AllowAnonymous]
        [X23Authorize(nameof(TestController), ButtonConst.Delete, ButtonConst.Update)]
        public async Task<bool> TestAuthByAdd()
        {
            return await Task.FromResult(true);
        }
        [HttpGet]
        [AllowAnonymous]
        [X23Authorize(nameof(TestController), ButtonConst.Query, ButtonConst.Update)]
        public async Task<bool> TestAuthByUpdate()
        {
            return await Task.FromResult(true);
        }
        [HttpGet]
        [AllowAnonymous]
        [X23Authorize(nameof(TestController), ButtonConst.Delete)]
        public async Task<bool> TestAuthByDelete()
        {
            return await Task.FromResult(true);
        }

        #region TestCap
        /// <summary>
        /// 测试Cap 发送消息
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<bool> TestCapPublishByController(string str)
        {
            _capPublisher.Publish("TestCapSubscribe", str);
            return await Task.FromResult(true);
        }
        /// <summary>
        /// 测试Cap 发送消息
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<bool> TestCapPublishByService(string str)
        {
            _capPublisher.Publish("TestCapSubscribeByService", str);
            return await Task.FromResult(true);
        }
        [HttpGet]
        [AllowAnonymous]
        public async Task<bool> TestCapPublishByServiceNoCAP(string str)
        {
            await _testManagementService.TestCapSubscribeByService(str);
            return await Task.FromResult(true);
        }
        [HttpGet]
        [AllowAnonymous]
        [CapSubscribe("TestCapSubscribe")]
        public async Task TestCapSubscribe(string str)
        {
            var data = await _userManagementService.GetByIdAsync(1);
            Console.WriteLine($"{str}+{data.ToJson()}");
        }
        #endregion
    }
}
